package com.easipass.sys.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Value("${ep.settings.corsSwitch:true}")
    Boolean corsSwitch;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        if(corsSwitch){
            registry.addMapping("/**")
                    .allowedOrigins("*")
                    .allowCredentials(true)
                    .allowedMethods("GET", "POST", "DELETE", "PUT" , "OPTIONS")
                    .maxAge(3600);
        }
    }

    @Override
    public void addViewControllers( ViewControllerRegistry registry ) {
        registry.addViewController( "/" ).setViewName( "sys/index" );
        registry.setOrder( Ordered.HIGHEST_PRECEDENCE );
    }
}
